﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using UniversityStudentManagemnetSystem.DAO.DAL;
using UniversityStudentManagemnetSystem.DAO.DAL.Gateway;

namespace UniversityStudentManagemnetSystem.BLL
{
    public class ScheduleManager
    {
        protected enum Rows
        {
            oneRowAffected = 1,
            noRowAffected = 0,
            oneRowSelected = 1,
            noRowSeleced = 0,
            oneRowCounted = 1,
            noRowCounted = 0
        }
        private ScheduleGateway scheduleGateway;
        public ScheduleManager()
        {
            scheduleGateway=new ScheduleGateway();
        }
        public string SaveClassSchedule(CourseSchedule courseSchedule)
        {

            DataTable scheduledCourseTable =
            scheduleGateway.GetAllScheduledCourseForId(courseSchedule.Building.Id, courseSchedule.Room.Id, courseSchedule.Day);
            bool timeIsFree = TimeChekaing(courseSchedule, scheduledCourseTable);
            if (timeIsFree)
            {
                if (scheduleGateway.ClassScheduleSave(courseSchedule))
                {
                    return "The course is scheduled, as you requisted"; 
                }
                else
                {
                    return "error";
                }
                
            }
            else
            {
                return "Time is already booked";
            }
              
        }

        private bool TimeChekaing(CourseSchedule courseSchedule, DataTable scheduledCourseTable)
        {
            DateTime requestStartTime = Convert.ToDateTime(courseSchedule.Times.StartingTime);
            DateTime requestEndingTime = Convert.ToDateTime(courseSchedule.Times.EndingTime);
            DateTime bookedStartingTime;
            DateTime bookedEndingTime;

            bool timeIsFree = true;

             if (scheduledCourseTable.Rows.Count >= (int)Rows.oneRowCounted)
            {
                foreach (DataRow row in scheduledCourseTable.Rows)
                {
                    bookedStartingTime = Convert.ToDateTime(row["StartingTime"]);
                    bookedEndingTime = Convert.ToDateTime(row["EndingTime"]);

                    if ((bookedStartingTime <= requestStartTime) && (requestStartTime < bookedEndingTime))
                    {
                        timeIsFree = false;
                        break;
                    }
                    else if ((bookedStartingTime < requestEndingTime) && (requestEndingTime <= bookedEndingTime))
                    {
                        timeIsFree = false;
                        break;
                    }
                    else
                    {
                        timeIsFree = true;
                    }
                }
            }
            else
            {
                timeIsFree = true;
            }

            return timeIsFree;
        }
    }
}